<div class="content-wrapper">
    <section class="content-header">
        <h1>{$itemInfo?'编辑':'新增'}产品</h1>
        <ol class="breadcrumb">
            <li><a href="/"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">项目管理</li>
            <li class="active">{$itemInfo?'编辑':'新增'}产品</li>
        </ol>
    </section>
    <section class="content">
        <div class="box">
            <div class="box-header with-border">
            </div>
            <div class="box-body">
                <form class="form-horizontal" role="form" action="javascrip:;" method="post">
                    <input type="hidden" name="id" id="id" value="{$itemInfo.id}" />
                    <div class="form-group">
                        <label for="name" class="col-sm-3 control-label"> 产品名称 </label>
                        <div class="col-sm-6">
                            <input class="form-control" id="name" name="name" value="{$itemInfo.name}"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="parent_cid" class="col-sm-3 control-label"> 一级分类 </label>
                        <div class="col-sm-6">
                            <select id="parent_cid" name="parent_cid" class="form-control">
                                <option value="0">请选择</option>
                                <foreach name="one_cate" item="one">
                                    <option value="{$one.id}" <if condition="$one.id eq $itemInfo.parent_cid ">selected</if>>{$one.name}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="second_cid" class="col-sm-3 control-label"> 二级分类 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="second_cid" name="second_cid">
                                <option value="0">请选择</option>
                                <foreach name="two_cate" item="two">
                                    <option value="{$two.id}" <if condition="$two.id eq $itemInfo.second_cid ">selected</if>>{$two.name}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="three_cid" class="col-sm-3 control-label"> 三级分类 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="three_cid" name="three_cid">
                                <option value="0">请选择</option>
                                <foreach name="three_cate" item="three">
                                    <option value="{$three.id}" <if condition="$three.id eq $itemInfo.three_cid ">selected</if>>{$three.name}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="status" class="col-sm-3 control-label"> 状态 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="status" name="status">
                                <option value="1" <if condition="$itemInfo.status eq 1 ">selected</if>>正常</option>
                                <option value="-1" <if condition="$itemInfo.status eq -1 ">selected</if>>关闭</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="put_port" class="col-sm-3 control-label">投放端口</label>
                        <div class="col-sm-6">
                            <input id="put_port" type="hidden" name="put_port" value="{$itemInfo.put_port}"/>
                            <div id="put_port_copy">
                                <label class="col-sm-pull-1">
                                    <input type="checkbox" name="put_port" class="minimal-red" value="pc" <php>if(in_array('pc', explode(',',$itemInfo["put_port"]))):echo checked; endif;</php>>
                                    PC
                                </label>
                                <label class="col-sm-pull-1">
                                    <input type="checkbox" name="put_port" class="minimal-red" value="ios" <php>if(in_array('ios', explode(',',$itemInfo["put_port"]))):echo checked; endif;</php>>
                                    IOS
                                </label>
                                <label class="col-sm-pull-1">
                                    <input type="checkbox" name="put_port" class="minimal-red" value="android" <php>if(in_array('android', explode(',',$itemInfo["put_port"]))):echo checked; endif;</php>>
                                    Android
                                </label>
                                <label class="col-sm-pull-1">
                                    <input type="checkbox" name="put_port" class="minimal-red" value="智能版" <php>if(in_array('智能版', explode(',',$itemInfo["put_port"]))):echo checked; endif;</php>>
                                    智能版
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="put_place" class="col-sm-3 control-label">投放位置</label>
                        <div class="col-sm-6">
                            <input id="put_place" type="hidden" name="put_place" value="{$itemInfo.put_place}"/>
                            <div id="put_place_copy">
                                <label class="col-sm-pull-1">
                                    <input type="checkbox" name="put_place" class="minimal-red" value="frs" <php>if(in_array('frs', explode(',',$itemInfo["put_place"]))):echo checked; endif;</php>>
                                    FRS
                                </label>
                                <label class="col-sm-pull-1">
                                    <input type="checkbox" name="put_place" class="minimal-red" value="pb" <php>if(in_array('pb', explode(',',$itemInfo["put_place"]))):echo checked; endif;</php>>
                                    PB
                                </label>
                                <label class="col-sm-pull-1">
                                    <input type="checkbox" name="put_place" class="minimal-red" value="无" <php>if(in_array('无', explode(',',$itemInfo["put_place"]))):echo checked; endif;</php>>
                                    无
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="is_enquiry" class="col-sm-3 control-label"> 询价 </label>
                        <div class="col-sm-6">
                            <select class="form-control" id="is_enquiry" name="is_enquiry">
                                <option value="2" <if condition="$itemInfo.is_enquiry eq 2 ">selected</if>>否</option>
                                <option value="1" <if condition="$itemInfo.is_enquiry eq 1 ">selected</if>>是</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="form-field-1" class="col-sm-3 control-label"></label>
                        <div class="col-sm-6">
                            <button type="button" class="btn btn-info btn-sm save-btn">提交</button>
                            &nbsp;
                            <button type="reset" class="btn btn-sm">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>
<script type="text/javascript">
    function show_second(p_id) {
        var pid = p_id;
        $("#second_cid").empty();
        var str = "<option value='0'>请选择</option>";
        $.post('/admin/itemCate/ajaxGetCateList',{
            pid: pid
        },function (data) {
            if(data.Code==999){
                console.log(data.Data);
                //从服务器获取数据进行绑定
                $.each(data.Data, function (i, item) {
                    str += "<option value=" + item.id;
                    str += "";
                    str += ">" + item.name+ "</option>";
                });
                //将数据添加到这个下拉框里面
                $("#second_cid").append(str);
            }else{
                $("#second_cid").append(str);
                return false;
            }
        },"json");
    }
    function show_three(p_id) {
        var pid = p_id;
        $("#three_cid").empty();
        var str = "<option value='0'>请选择</option>";
        $.post('/admin/itemCate/ajaxGetCateList',{
            pid: pid
        },function (data) {
            if(data.Code==999){
                console.log(data.Data);
                //从服务器获取数据进行绑定
                $.each(data.Data, function (i, item) {
                    str += "<option value=" + item.id;
                    str += "";
                    str += ">" + item.name+ "</option>";
                });
                //将数据添加到这个下拉框里面
                $("#three_cid").append(str);
            }else{
                $("#three_cid").append(str);
                return false;
            }
        },"json");
    }
    $(function () {
        $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
            checkboxClass: 'icheckbox_minimal-red',
            radioClass: 'iradio_minimal-red'
        });
        //二级分类
        $("#parent_cid").change(function () {
            var pid = $(this).val();
            show_second(pid);
            show_three(0);
        });
        //三级分类
        $("#second_cid").change(function () {
            var pid = $(this).val();
            show_three(pid);
        });
        //  投放端口
        $('input[name="put_port"]').on('ifChecked',function (event) {
            var strObj = $("#put_port").val();
            var strV = event.currentTarget.defaultValue;
            console.info(strObj);
            if(!strObj){
                strObj = new Array();
            }else {
                strObj = strObj.split(',')
            }
            strObj.push(strV);
            strObj.join(',');
            $("#put_port").val(strObj);
        }).on('ifUnchecked',function (event) {
            var strObj = $("#put_port").val();
            var strV = event.currentTarget.defaultValue;
            if(!strObj){
                strObj = new Array();
            }else {
                strObj = strObj.split(',')
            }
            strObj.splice($.inArray(strV, strObj), 1);
            strObj.join(',');
            $("#put_port").val(strObj);
        });
        //  投放位置
        $('input[name="put_place"]').on('ifChecked',function (event) {
            var strObj = $("#put_place").val();
            var strV = event.currentTarget.defaultValue;
            if(!strObj){
                strObj = new Array();
            }else {
                strObj = strObj.split(',')
            }
            strObj.push(strV);
            strObj.join(',');
            $("#put_place").val(strObj);
        }).on('ifUnchecked',function (event) {
            var strObj = $("#put_place").val();
            var strV = event.currentTarget.defaultValue;
            if(!strObj){
                strObj = new Array();
            }else {
                strObj = strObj.split(',')
            }
            var index = $.inArray(strV, strObj);
            if(index>-1){
                strObj.splice($.inArray(strV, strObj), 1);
            }
            strObj.join(',');
            $("#put_place").val(strObj);
        });
        //提交
        $(".save-btn").click(function () {
            var id = $("#id").val();
            var parent_cid = $("#parent_cid").val();
            var second_cid = $("#second_cid").val();
            var three_cid = $("#three_cid").val();
            var is_enquiry = $("#is_enquiry").val();
            var status = $("#status").val();
            var name = $.trim($("#name").val());
            var put_port = $.trim($("#put_port").val());
            var put_place = $.trim($("#put_place").val());
            if(parent_cid==0){
                ArtDialogHlp.showInfo('提示',"请选这产品一级分类");
                return false;
            }
            if(second_cid==0){
                ArtDialogHlp.showInfo('提示',"请选这产品二级分类");
                return false;
            }
            if(three_cid==0){
                ArtDialogHlp.showInfo('提示',"请选这产品三级分类");
                return false;
            }
            if(name==''){
                ArtDialogHlp.showInfo('提示',"请填写产品名称");
                return false;
            }
            $.post('/admin/item/ajaxSaveItem',{
                id: id,
                parent_cid: parent_cid,
                second_cid: second_cid,
                three_cid: three_cid,
                status: status,
                name: name,
                put_port: put_port,
                put_place: put_place,
                is_enquiry:is_enquiry
            },function (data) {
                if(data.Code == 999) {
                    ArtDialogHlp.showInfo('提示', '操作成功', function(){
                        window.location.href = '/admin/Item/itemList';
                    });
                } else {
                    ArtDialogHlp.showInfo('提示', data.Msg);
                }
            },"JSON");
        })
    })
</script>